const exp = require('express'),
    fs = require('fs'),
    bodyParser = require('body-parser'),    
    regist = exp.Router();

regist.post('/api/register/user', (req, res) => {
    if (req.body.psw == req.body.repsw) {
        fs.exists('user', (exists) => {
            if (!exists) {
                fs.mkdir('user')
            } else {
                fs.exists(`user/${req.body.userName}.json`, (exists) => {
                    if (!exists) {
                        fs.createWriteStream(`user/${req.body.userName}.json`)
                        fs.writeFile(`user/${req.body.userName}.json`, JSON.stringify(req.body), (err) => {
                            if (err) {
                                res.status(200).json({
                                    code: 'error',
                                    message: '注册失败'
                                })
                            } else {
                                res.status(200).json({
                                    code: 'success',
                                    message: '注册成功'
                                })
                            }
                        })
                    } else {
                        res.status(200).json({
                            code: 'error',
                            message: '用户名已经被注册'
                        })
                    }
                })
            }
        })
    } else {
        res.status(200).json({
            code: 'error',
            message: '两次输入密码不一致'
        })
    }
})
module.exports = regist;